<?php

namespace app\admin\controller;


use think\exception\ValidateException;

class AdPosition extends Base
{
    /**
     * index #广告位置列表
     *
     * @auth kouweihong
     * @date 2020/12/3
     */
    public function index()
    {
        //获取参数
        $title = input('title');

        $where = [['status', '>=', \app\common\model\AdPosition::STATUS_0]];
        $title && $where[] = ['title', 'like', '%' . $title . '%'];
        $list = \app\common\model\AdPosition::where($where)->order('id desc')->paginate($this->pageSize);
        $list->pageSize = 2;

        $this->success($list);
    }

    /**
     * add #添加广告位置
     * api post /{controller}/{method}
     *
     * @auth kouweihong
     * @date 2020/12/3
     */
    public function add()
    {
        $this->_save();
    }

    /**
     * edit #编辑广告位置
     * api post /{controller}/{method}
     *
     * @auth kouweihong
     * @date 2020/12/3
     */
    public function edit()
    {
        $this->_save();
    }

    /**
     * save #
     *
     * @auth kouweihong
     * @date 2020/12/3
     */
    private function _save()
    {
        $this->request->isPost() || $this->error('非法操作');
        $params = input('post.');
        $params || $this->error('提交参数为空');

        try {
            $params['user_type'] = $this->auth->user_type;
            $params['user_id'] = $this->auth->id;
            validate(\app\common\validate\AdPosition::class)->check($params);

            isset($params['id']) ? \app\common\model\AdPosition::updateRecord($params) : \app\common\model\AdPosition::createRecord($params);

            $this->success();
        } catch (ValidateException $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * read #读取信息
     * api post /{controller}/{method}
     * @param int id 广告位置ID
     *
     * @auth kouweihong
     * @date 2020/12/3
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function read($id)
    {
        //获取参数
        $id || $id = input('id');
        $id || $this->error('参数错误');

        //查询
        $info = \app\common\model\AdPosition::find($id);
        $info || $this->error('信息记录不存在');
        $info->status == \app\common\model\AdPosition::STATUS_F_1 && $this->error('数据已删除，信息不存在');

        $this->success($info);
    }

    /**
     * delete #删除广告位置
     * api post /{controller}/{method}
     * @param int id 广告位置ID
     *
     * @param $id
     * @auth kouweihong
     * @date 2020/12/3
     */
    public function del($id)
    {
        //获取参数
        $id || $id = input('id');
        $id || $this->error('参数错误');

        //查询
        $info = \app\common\model\AdPosition::find($id);
        $info || $this->error('信息记录不存在');
        $info->status == \app\common\model\AdPosition::STATUS_F_1 && $this->error('数据已删除，信息不存在');

        //保存
        $res = $info->save(['status' => \app\common\model\AdPosition::STATUS_F_1]);
        $res === false && $this->error('操作失败');

        $this->success();
    }
}
